package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @PostMapping("/submit")
    @Transactional
    public R<String> submit(@RequestBody OrdersDto ordersDto){
        log.info("去支付...");
        ordersService.saveWithOrderDetail(ordersDto);

        return R.success("下单成功");
    }


    /**
     * 后台分页查询所有用户的订单信息
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> ordersDtoR (Integer page, Integer pageSize, String number, String beginTime ,String endTime){
        log.info("分页查询订单信息......");
        //构建分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //将ordersPage拷贝到ordersDtoPage中
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        //构建查询条件
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(number != null,Orders::getNumber,number);
        queryWrapper.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        queryWrapper.le(endTime!=null,Orders::getOrderTime,endTime);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //调用分页查询
        ordersService.page(ordersPage,queryWrapper);

        //将ordersPage中的records遍历出来
        List<Orders> records = ordersPage.getRecords();
        //此时的records中只有id没有名称
        //新建一个OrdersDto集合用于最终的返回
        List<OrdersDto> ordersDtoList = null;
        ordersDtoList = records.stream().map((item)->{
            //新建一个OrdersDto对象
            OrdersDto ordersDto = new OrdersDto();
            //将ordersDto中的数据补全,此时ordersDot中没有userName属性
            BeanUtils.copyProperties(item,ordersDto);
            //获取records中的id
            Long userId = item.getUserId();
            //在根据userid查询用户名称
            User user = userService.getById(userId);
            String userName = user.getName();
            //再将userName赋值给ordersDto
            ordersDto.setUserName(userName);
            //返回ordersDto
            return ordersDto;
        }).collect(Collectors.toList());
        //再将ordersDtoList赋值给ordersDtoPage
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    /**
     * 用户订单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(Integer page,Integer pageSize){
        log.info("用户订单分页查询... page:{}   pageSize:{}",page,pageSize);
        /*//获取当前登录的用户id
        Long userId = BaseContext.getCurrentId();
        //构建分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //构造条件查询器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage,queryWrapper);
        return R.success(ordersPage);*/

        log.info("分页查询订单信息......");
        //获取当前登录的用户id
        Long userId = BaseContext.getCurrentId();
        //构建分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //将ordersPage拷贝到ordersDtoPage中
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        //构建查询条件
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //调用分页查询
        ordersService.page(ordersPage,queryWrapper);
        //将ordersPage中的records遍历出来
        List<Orders> records = ordersPage.getRecords();
        //此时的records中只有id没有名称
        //新建一个OrdersDto集合用于最终的返回
        List<OrdersDto> ordersDtoList = null;
        ordersDtoList = records.stream().map((item)->{
            //新建一个OrdersDto对象
            OrdersDto ordersDto = new OrdersDto();
            //将ordersDto中的数据补全,此时ordersDot中没有userName属性
            BeanUtils.copyProperties(item,ordersDto);
            //在根据userid查询用户名称
            User user = userService.getById(userId);
            String userName = user.getName();
            //再将userName赋值给ordersDto
            ordersDto.setUserName(userName);
            //返回ordersDto
            return ordersDto;
        }).collect(Collectors.toList());
        //再将ordersDtoList赋值给ordersDtoPage
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    /**
     * 更改订单状态
     * @return
     */
    @PutMapping
    public R<String> put(@RequestBody Orders orders){
        log.info("更改订单状态。。。。 orders：{}",orders);
        //调用service层
        ordersService.updateById(orders);
        return R.success("修改成功");

    }
}
